from microbit import *
from time import sleep_us
from machine import time_pulse_us

class Ultrasonic(object):

    def __init__(self, trig_pin, echo_pin):
        self.trigPin = trig_pin
        self.echoPin = echo_pin

    def read_distance(self, unit = 0):
        self.echoPin.read_digital()
        self.trigPin.write_digital(1)
        sleep_us(10)
        self.trigPin.write_digital(0)
        ts = time_pulse_us(self.echoPin, 1, 23333)

        if unit == 0:
            return ts * 0.0343 // 2
        elif unit == 1:
            return ts * 0.0135 // 2